package csci5115.autoalarm;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.ToggleButton;

public class EventManager extends Activity {

	public DatePicker datePicker;
	public TimePicker timePicker;
	public EditText title, loc;
	public ToggleButton alarm, transCar, transBus, transBike, transWalk;;
	public Button save, delete;
	public byte trans = 0;
	public boolean editing = false;
	public int mId = -1;
	private static int MOCK_ALARM_INTERVAL = 1;
	//Mock request code, should be different for each alarm
	private static int ALARM_REQUEST_CODE = 1283798;
	public Location location;
	public String locname;
	public Geocoder geoCoder;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eventmanager);
		datePicker = (DatePicker) findViewById(R.id.datepick_datePicker);
		timePicker = (TimePicker) findViewById(R.id.timepick_timePicker);
		title = (EditText) findViewById(R.id.edittext_eventTitle);
		loc = (EditText) findViewById(R.id.edittext_location);
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll_transportationLayout);
		transCar = (ToggleButton) ll.findViewById(R.id.button_transCar);
		transBus = (ToggleButton) ll.findViewById(R.id.button_transBus);
		transBike = (ToggleButton) ll.findViewById(R.id.button_transBike);
		transWalk = (ToggleButton) ll.findViewById(R.id.button_transWalk);
		alarm = (ToggleButton) findViewById(R.id.togbutton_alarmStatus);
		save = (Button) findViewById(R.id.button_saveEvent);
		delete = (Button) findViewById(R.id.button_deleteEvent);
		Intent i = getIntent();
		if (i.hasExtra("date")) {
			Date startDate = new Date(i.getExtras().getLong("date", 12341));
			datePicker.init(startDate.getYear() + 1900, startDate.getMonth(),
					startDate.getDate(), null);
		}

		// if already an event
		if (i.hasExtra("title")) {
			// set title
			title.setText(i.getExtras().getString("title"));
			// set time
			Date time = new Date(i.getExtras().getLong("time", 12341));
			timePicker.setCurrentHour(time.getHours());
			timePicker.setCurrentMinute(time.getMinutes());
			// set alarm status
			alarm.setChecked(i.getExtras().getBoolean("alarm"));
			mId = i.getExtras().getInt("id");
			editing = true;
			trans = i.getExtras().getByte("trans");
			if ((trans & Transportation.CAR) != 0) {
				transCar.setChecked(false);
			}
			if ((trans & Transportation.BUS) != 0) {
				transBus.setChecked(true);
			}
			
			if ((trans & Transportation.BIKE) != 0){
				transBike.setChecked(true);
			}
			if ((trans & Transportation.WALK) != 0){
				transWalk.setChecked(true);
			}
			
			locname = i.getExtras().getString("location");
			loc.setText(locname);
			
		}

		// Save Event
		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				saveEvent();
			}

		});

		//Delete Event
		delete.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				deleteEvent();
			}

		});

	}

	public void deleteEvent() {
		if(mId < 0) {
			finish();
		} else {
			EventDBAdapter db = new EventDBAdapter(this);
			db.open();
			db.delete(mId);
			db.close();
			finish();
		}
		
	}
	public void saveEvent() {
		EventDBAdapter db = new EventDBAdapter(this);
		String t = title.getText().toString();
		Date date = new Date(111, datePicker.getMonth(),
				datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
				timePicker.getCurrentMinute());
		Boolean status = alarm.isChecked();
		String type = "Vibrate";
		locname = loc.getText().toString();
		updateTransportationByte();
		db.open();
		
		if (editing) {
			AAEvent e1 = new AAEvent(mId, t, date, status, type, trans, locname);
			db.replace(e1);
		} else {
			//new event
			AAEvent e1 = new AAEvent(t, date, status, type, trans, locname);
			db.insert(e1);
		}
		db.close();
		
		//Fake alarm for demo purposes
		addEventToAlarmSchedule();
		
		finish();
		
		
	}

	private void updateTransportationByte() {
		if(transCar.isChecked()) {
			trans = (byte) (trans + Transportation.CAR);
		}
		
		if(transBus.isChecked()) {
			trans = (byte) (trans + Transportation.BUS);
		}
		
		if(transWalk.isChecked()) {
			trans = (byte) (trans + Transportation.WALK);
		}
		
		if(transBike.isChecked()) {
			trans = (byte) (trans + Transportation.BIKE);
		}
	}
	public void addEventToAlarmSchedule() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, MOCK_ALARM_INTERVAL);
		Intent intent = new Intent(this, AlarmReceiver.class);
		intent.putExtra("title", title.getText().toString());
		intent.putExtra("location", location);
		String t = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute();
		intent.putExtra("time", t);
		PendingIntent sender = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
	}
	
//	public void searchGoogleMaps() throws IOException {
//		Dialog dialog = new Dialog(this);
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setTitle("Select Location");
//		
//		LinearLayout linlay = new LinearLayout(this);
//		location = loc.getText().toString();
//		List<Address> addresses = geoCoder.getFromLocationName(location,5);
//		
//		ListView addressList = new ListView(this);
//		
//		SimpletAdapter listAdapter = new SimpleAdapter(this, addressList, );
//		addressList.setAdapter(listAdapter);
//		
//		
//		builder.setView(linlay);
//		dialog = builder.create();
//	}
}
